package com.example.account.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.account.service.AccountService;
import com.example.commons.response.ResponseResult;
import com.example.commons.response.RestResultUtil;
import com.example.commons.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/private")
public class ApiController {
    @Autowired
    AccountService accountService;

    //注册时添加账户
    @PostMapping("/add/account")
    public ResponseResult addAccount(@RequestBody String body) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        accountService.addAccount(jsonObject.getString("userId"));
        return new ResponseResult(ResultCode.SUCCESS, null, null);
    }

    //操作用户账户余额
    @PostMapping("/oper/bal")
    public ResponseResult operBal(@RequestBody String body) {
        JSONObject data = JSONObject.parseObject(body);
        accountService.operBal(data.getString("userId"), data.getString("balance"), data.getIntValue("payType"),
                data.getIntValue("type"), data.getString("remarks"), data.getString("explains"));
        return new ResponseResult(ResultCode.SUCCESS, null, null);
    }

    @PostMapping("/test")
    public ResponseResult  test (@RequestBody String userId) throws InterruptedException {

        return RestResultUtil.success(accountService.selectUserAccount(userId));
    }
}
